﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using WpfApplication1;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void b_pretraga_Click(object sender, RoutedEventArgs e)
        {
            Thread thread1 = new Thread(new ThreadStart(prikazi));
            thread1.Start();
        }

        void prikazi()
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                HtmlAgilityPack.HtmlWeb docHFile = new HtmlAgilityPack.HtmlWeb();

                htmlDoc = docHFile.Load("http://www.dict.cc/?s=" + t_rijec.Text);

                htmlDoc.OptionFixNestedTags = true;
                if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
                {
                    
                }
                else
                {

                    if (htmlDoc.DocumentNode != null)
                    {
                        HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
                        Rezultat r = new Rezultat("","");
                        if (bodyNode != null)
                        {
                            int a = 1;
                            foreach (HtmlAgilityPack.HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//td[@class='td7nl']"))
                            {
                                String prijevod = "";
                                foreach (HtmlAgilityPack.HtmlNode node1 in node.ChildNodes)
                                {
                                    double k;
                                    if (!double.TryParse(node1.InnerText, out k))
                                    {
                                        if (node1.InnerText == "&nbsp;")
                                            prijevod += " ";
                                        else
                                            prijevod += Convert.ToString(node1.InnerText);//node1.ChildNodes[0].InnerHtml;
                                    }
                                }

                                if (a == 1)
                                {
                                    r = new Rezultat(prijevod, "");
                                    a = 2;
                                }
                                else
                                {
                                    r.Engleski = prijevod;
                                    a = 1;
                                }

                                if (a == 1) dg_prva.Items.Add(r);
                            }
                           

                        }
                    }
                }
            }));

            this.Dispatcher.Invoke((Action)(() =>
            {
                HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument();
                HtmlAgilityPack.HtmlWeb docHFile = new HtmlAgilityPack.HtmlWeb();

                htmlDoc = docHFile.Load("http://dict.leo.org/ende/index_en.html#/search=suchen&searchLoc=0&resultOrder=basic&multiwordShowSingle=on");

                htmlDoc.OptionFixNestedTags = true;
                if (htmlDoc.ParseErrors != null && htmlDoc.ParseErrors.Count() > 0)
                {
                    
                    MessageBox.Show("jnK");
                }
                else
                {
                    MessageBox.Show("oK");
                    if (htmlDoc.DocumentNode != null)
                    {
                        HtmlAgilityPack.HtmlNode bodyNode = htmlDoc.DocumentNode.SelectSingleNode("//body");
                        Rezultat r = new Rezultat("", "");
                        if (bodyNode != null)
                        {
                            int a = 1;
                            foreach (HtmlAgilityPack.HtmlNode node in htmlDoc.DocumentNode.SelectNodes("//td[@class='text']"))
                            {
                                String prijevod = "";
                                MessageBox.Show("jk");
                             /*   foreach (HtmlAgilityPack.HtmlNode node1 in node.ChildNodes)
                                {
                                    double k;
                                    if (!double.TryParse(node1.InnerText, out k))
                                    {
                                        if (node1.InnerText == "&nbsp;")
                                            prijevod += " ";
                                        else
                                            prijevod += Convert.ToString(node1.InnerText);//node1.ChildNodes[0].InnerHtml;
                                    }
                                }

                                if (a == 1)
                                {
                                    r = new Rezultat(prijevod, "");
                                    a = 2;
                                }
                                else
                                {
                                    r.Engleski = prijevod;
                                    a = 1;
                                }

                                if (a == 1) dg_prva.Items.Add(r);*/
                                FlowDocument myFlowDoc = new FlowDocument();

                                // Add paragraphs to the FlowDocument.
                                myFlowDoc.Blocks.Add(new Paragraph(new Run("node.InnerHtml")));
                                myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 2")));
                                myFlowDoc.Blocks.Add(new Paragraph(new Run("Paragraph 3")));
                                rb.Document = myFlowDoc;
                            }


                        }
                    }
                }
            }));
        }
    }
}
